package org.leumasjaffe.initiativeTracker.distributed.controller;

import java.util.Hashtable;

import org.eclipse.jdt.annotation.NonNull;

import edu.wpi.heineman.xml.Message;
import edu.wpi.heineman.client.IMessageHandler;

/**
 * Sample implementation of a protocol handler to respond to messages received from the server.
 * You should follow this template when designing YOUR message handler.
 */
public final class ClientProtocolFactory implements edu.wpi.heineman.client.ClientProtocolFactory{
	
	static Hashtable<String, IMessageHandler> handlers =
				new Hashtable<String, IMessageHandler>();
	
	static {
		handlers.put("turnResponse", new ChangeTurnHandler());
		handlers.put("damageResponse", new DamageHandler());
		handlers.put("surgeResponse", new SurgeHandler());
		handlers.put("newResponse", new CharacterHandler());
		handlers.put("connectResponse", new ConnectHandler());
		handlers.put("initiativeResponse", new DownloadHandler());
		handlers.put("killResponse", new KillHandler());
		handlers.put("killAllResponse", new KillAllHandler());
		handlers.put("delayResponse", new DelayHandler());
		handlers.put("rollResponse", new RollHandler());
		handlers.put("nullResponse", new NullRollHandler());
	}
	
	@Override
	public synchronized void process(@NonNull Message response) {
		String type = response.contents.getFirstChild().getLocalName();
		IMessageHandler handler = handlers.get(type);
		if (handler != null)
			handler.newCopy().process(response);
		
//		System.out.println("Received:" + response);
	} 

}
